const app = getApp()
// pages/my/index.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isAuthorize: true, //是否已经授权登录
		userInfo: {},
		mag_balance:'0.00',
		is_wxpay:0,
		setting:{}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
          wx.hideTabBar();
		let that = this;
		app.connect('index/setting', {}, function (res) {
			that.setData({
				setting: res
			})
		});
		wx.getStorage({
			key: 'mag_balance',
			success: function (res) {
				that.setData({
					mag_balance:res.data
				});
			}
		})
		//是否开启微信支付
		app.connect('index/setting',{},function(res){
			that.setData({
				is_wxpay:res.is_wxpay,
				whatsMag:res.whatsMag
			})
		});
		//检查用户是否授权登录
		wx.getSetting({
			success(res) {
				//没有授权
				if (!res.authSetting['scope.userInfo']) {
					that.setData({
						isAuthorize: false
					});

				} else {
					that.setData({
						isAuthorize: true
					});

				}
			}
		})

		app.login_canno(function (userInfo) {
			if(userInfo != null) {
				that.setData({
					userInfo: userInfo.data
				})
				if (app.isPoneAvailable(userInfo.data.mobile)) {
					that.setData({
						isMobile: true
					})
				}

				//加载麦粒余额
				if (app.isPoneAvailable(that.data.userInfo.mobile)) { //手机号已绑定
					app.connect('account/getMagBalance',{
						uid:that.data.userInfo.uid,
						openid:that.data.userInfo.openId,
						mobile:that.data.userInfo.mobile
					},function(res){
						var balance = res.data.balance;

						that.setData({
							mag_balance: parseFloat(balance).toFixed(2)
						});
						wx.setStorage({
							key: "mag_balance",
							data: parseFloat(balance).toFixed(2)
						})
					});
				}
			}
			
		})
	},
	onShow:function(){
		this.onLoad();
	},
	getuserinfo: function (options) {
		let that = this;
		wx.showLoading({
			title: '加载中...',
		})
		wx.getSetting({
			success(res) {
				//授权
				if (res.authSetting['scope.userInfo']) {
					
					that.setData({
						isAuthorize: true
					});
					app.getUserInfo(function (userInfo) {
						//更新数据
						that.setData({
							userInfo: userInfo.data
						})
						wx.hideLoading()
					})
				}
			}
		})
		
	},
	wxLogin: function (func) {
		//调用登录接口
		//1.小程序调用wx.login得到code.
		wx.login({
			success: function (res) {
				var code = res['code'];
				//2.小程序调用wx.getUserInfo得到rawData, signatrue, encryptData.
				wx.getUserInfo({
					success: function (info) {
						console.log(info);
						var rawData = info['rawData'];
						var signature = info['signature'];
						var encryptData = info['encryptData'];
						var encryptedData = info['encryptedData'];
						var iv = info['iv'];

						//3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
						wx.request({
							url: constants.LOGIN_URL,
							data: {
								"code": code,
								"rawData": rawData,
								"signature": signature,
								"encryptData": encryptData,
								'iv': iv,
								'encryptedData': encryptedData
							},
							success: function (res) {
								if (res.statusCode != 200) {
									wx.showModal({
										title: '登录失败'
									});
								}
								typeof func == "function" && func(res.data);
							}
						});
					}
				});

			}
		});
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		this.onLoad();
		wx.stopPullDownRefresh()
	},
     /**
      * 转发邀请别人
      */
     onShareAppMessage: function (res) {
          let that = this;
          if (res.from === 'button') {
               // 来自页面内转发按钮
                //这里应该另外处理
               if (!app.isPoneAvailable(that.data.userInfo.mobile)) {
                    wx.showToast({
                         title: '还没有绑定手机号',
                    })
               }
               
               //假设绑定过了手机号
               return {
                    title: '帅哥美女潮人红人都在玩',
                    path: 'page/index/index'
               }
          }
          return {
               title: '帅哥美女潮人红人都在玩',
               path: 'page/index/index'
          }
     }
})